<?php
include_once "DBCORE.cls.php";
	class db_restaurant
	{
		private $tablename="tblrestaurant";
		private $db;
		public function __construct()
		{
			$this->db= new DBCORE();
			$this->db->db_connect('localhost','root','','restaurant');
			$this->db->db_query("SET NAMES 'UTF8'");
		}
		public function select($id)
		{
			$id = !Empty($id) ? " WHERE RestaurantID='{$id}'": "";
			$myStrSQL = "SELECT * FROM {$this->tablename} {$id}";
			$data = $this->db->db_fetch_assoc($this->db->db_query($myStrSQL));
			return $data;
		}
		public function selectAll()
		{
			$myStrSQL="SELECT * FROM {$this->tablename} WHERE 1";
			$result=$this->db->db_query($myStrSQL);
			$resArray = array();
			while ($row = $this->db->db_fetch_assoc($result)) {
				$resArray[] = $row;
			}
			return $resArray;
		}
		public function delete($id)
		{
			$id = !Empty($id) ? " WHERE RestaurantID='{$id}'": "";
			$myStrSQL="DELETE FROM {$this->tablename} {$id}";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		public function deleteAll()
		{
			$myStrSQL="DELETE FROM {$this->tablename} WHERE 1";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		public function insertRest(restaurant $new)
		{
			$id = $new->getID();
			$fullname = $new->getFullname();
			$description = $new->getDescription();
			$address = $new->getAddress();
			$place = $new->getPlace();
			$rate = $new->getRate();
			$myStrSQL = "INSERT INTO {$this->tablename} VALUES ('$id','$fullname','$description','$address','$place','$rate','0')";
			return ($this->db->db_query($myStrSQL));
		}
		public function insert(array $data)
		{
			$id = isset($data['ID'])?$data['ID']:"";
			if(!$this->checkExists($id) || empty ($id))
				return false;
			$fullname = $data['Fullname'];
			$description = $data['Description'];
			$address = $data['Address'];
			$place = $data['Place'];
			$rate = $data['Rate'];
			$myStrSQL = "INSERT INTO {$this->tablename} VALUES ('$id','$fullname','$description','$address','$place','$rate','0')";
			return ($this->db->db_query($myStrSQL));
		}
		//
		public function inserts(array $data)
		{
			foreach($data as $key => $value)
			{
				if($this->checkExists($value['ID']))
					return false;
				insert($value);
			} 
		}
		public function updates($update = array(), $condition)
		{
			if(empty($condition))
				return;
			$update = "FullName = '{$update['FullName']}',
					Description ='{$update['Description']}',
					Address='{$update['Address']}',
					Place='{$update['Place']}',
					Rate='{$update['Rate']}',
					NumberVote='{$update['NumberVote']}'";
			$condition = "RestaurantID = '{$condition}'";
			$myStrSQL = "UPDATE {$this->tablename} SET {$update} WHERE {$condition}";
			$this->db->db_query($myStrSQL);
			return $this->db->db_affected_rows();
		}
		//
		
		public function findbyCondition(array $data)
		{
			$myStrSQL = "SELECT * FROM {$this->tablename} WHERE 1";
			if(!empty($data['Id']))
				$myStrSQL = $myStrSQL." AND RestaurantID='".$data['Id']."'";
			if(!empty($data['Fullname']))
				$myStrSQL = $myStrSQL." AND FullName='".$data['Fullname']."'";
			if(!empty($data['Description']))
				$myStrSQL = $myStrSQL." AND Description='".$data['Description']."'";
			if(!empty($data['Address']))
				$myStrSQL = $myStrSQL." AND Address='".$data['Address']."'";
			if(!empty($data['Place']))
				$myStrSQL = $myStrSQL." AND Place='".$data['Place']."'";
			if(!empty($data['Rate']))
				$myStrSQL = $myStrSQL." AND Rate='".$data['Rate']."'";
			$result=$this->db->db_query($myStrSQL);
			$resArray = array();
			while ($row = $this->db->db_fetch_assoc($result)) {
				$resArray[] = $row;
			}
			return $resArray;
		}
		//
		
		public function checkExists($id='')
		{
			if(empty($id))
				return false;
			$myStrSQL = "SELECT * FROM {$this->tablename} WHERE RestaurantID ='{$id}'";
			return $this->db->db_query($myStrSQL);
			
		}
	}
?>